/*
 * @(#)PostReceiver.java 2017-1-12 下午12:58:34 showPost Maven Webapp Copyright
 * 2017 Thuisoft, Inc. All rights reserved. THUNISOFT PROPRIETARY/CONFIDENTIAL.
 * Use is subject to license terms.
 */
package com.happut.showPost.logic;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.happut.showPost.EchoWebSocketHandler;
import com.happut.showPost.utils.SpringContextUtil;

/**
 * PostReceiver
 * @author wfei
 * @time 2017-1-12下午12:58:34
 */
@Controller
@RequestMapping("/api")
public class PostReceiver {

    @RequestMapping(value = "post", method = RequestMethod.POST)
    @ResponseBody
    public String handlePost(@RequestBody String bodyParam, HttpServletRequest request, HttpServletResponse response) {
        EchoWebSocketHandler handle = (EchoWebSocketHandler) SpringContextUtil.getBean("echoWebSocketHandler");
        StringBuilder message = new StringBuilder();
        message.append("<b>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        message.append("</b>&nbsp;接收到来自：<b>");
        message.append(request.getRemoteAddr());
        message.append("</b>&nbsp;发来的POST请求，<br/><b>body</b>内容为：<br/><code>");
        message.append(StringEscapeUtils.escapeHtml(bodyParam));
        message.append("</code>");
        message.append("<br/><b>header</b>信息为：<br/><code>{");

        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String param = enumeration.nextElement();
            message.append("\"" + param + "\":\"" + request.getHeader(param) + "\",");
        }

        message.append("}</code><br/>");
        handle.sendMessageToUsers(message.toString());
        return "ok";
    }

    @RequestMapping(value = "post/{id}")
    @ResponseBody
    public String delete(@PathVariable String id, @RequestBody String bodyParam, HttpServletRequest request, HttpServletResponse response) throws Exception {
        EchoWebSocketHandler handle = (EchoWebSocketHandler) SpringContextUtil.getBean("echoWebSocketHandler");
        StringBuilder message = new StringBuilder();
        message.append("<h5>" + id + "</h5><hr/><b>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        message.append("</b>&nbsp;接收到来自：<b>");
        message.append(request.getRemoteAddr());
        message.append("</b>&nbsp;发来的POST请求，<br/><b>body</b>内容为：<br/><code>");
        message.append(StringEscapeUtils.escapeHtml(bodyParam));
        message.append("</code>");
        message.append("<br/><b>header</b>信息为：<br/><code>{");

        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String param = enumeration.nextElement();
            message.append("\"" + param + "\":\"" + request.getHeader(param) + "\",");
        }

        message.append("}</code><br/>");
        handle.sendMessageToUsers(message.toString());
        return "ok";
    }
}
